package com.think.teaching.controller;

import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.College;
import com.think.teaching.service.impl.CollegeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 院系 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Controller
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "院系管理接口")
public class CollegeController {

	private final CollegeServiceImpl collegeService;

	public CollegeController(CollegeServiceImpl collegeService) {
		this.collegeService = collegeService;
	}

	@ApiOperation("添加新的院系")
	@PostMapping("admin/college")
	public Result AddNewCollege(@RequestBody() College data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			College college = collegeService.AddNewCollege(data);
			if (college != null) {
				return Result.success(college);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}

	@ApiOperation("更新的院系")
	@RequestMapping(value = "admin/college", method = RequestMethod.PUT)
	public Result UpdateCollege(@RequestBody() College data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
			College college = collegeService.UpdateCollege(data);
			if (college != null) {
				return Result.success(college);
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}

	@ApiOperation("删除院系")
	@RequestMapping(value = "admin/college/{id}", method = RequestMethod.DELETE)
	public Result DeleteCollege(@PathVariable("id") Integer id) {
		College college = collegeService.getById(id);
		if (college == null) {
			return Result.failure(ResultCode.COLLEGE_NOT_EXIST);
		} else {
			if (collegeService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}

	@ApiOperation("管理员获取院系详情")
	@RequestMapping(value = "admin/college/{id}", method = RequestMethod.GET)
	public Result GetCollege(@PathVariable("id") Integer id) {
		College college = collegeService.getById(id);
		if (college == null) {
			return Result.failure(ResultCode.COLLEGE_NOT_EXIST);
		} else {

			return Result.success(college);
		}
	}

	@ApiOperation("管理员修改院系状态")
	@RequestMapping(value = "admin/college/state", method = RequestMethod.PUT)
	public Result UpdateCollegeState(@RequestParam("id") Integer id, @RequestParam("state") Integer state) {
		College college = collegeService.getById(id);
		if (college == null) {
			return Result.failure(ResultCode.COLLEGE_NOT_EXIST);
		} else {
			try {
				college = collegeService.SetCollegeState(id, state);
				return Result.success(college);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}

	@ApiOperation("用户搜索院系")
	@RequestMapping(value = "user/college/search", method = RequestMethod.GET)
	public Result UserSearchCollege(@RequestParam("key") String key) {
		return Result.success(collegeService.SearchCollegeWithKey(key, true));
	}
	
	@ApiOperation("用户获取院系下的所有专业")
	@RequestMapping(value = "user/college/majors/{id}", method = RequestMethod.GET)
	public Result UserGetCollegeMajors(@PathVariable("id") Integer id) {
		College college = collegeService.getById(id);
		if (college == null) {
			return Result.failure(ResultCode.COLLEGE_NOT_EXIST);
		} else {
			try {
				return Result.success(collegeService.getCollegeMajors(id));
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}

	@ApiOperation("管理员搜索院系")
	@RequestMapping(value = "admin/college/search", method = RequestMethod.GET)
	public Result AdminSearchCollege(@RequestParam("key") String key) {
		return Result.success(collegeService.SearchCollegeWithKey(key, true));
	}

	@ApiOperation("管理员搜索院系列表")
	@RequestMapping(value = "admin/colleges/{page}/{count}", method = RequestMethod.GET)
	public Result AdminListCollege(@PathVariable("page") Integer page, @PathVariable("count") Integer count,
			@RequestParam(value = "school",required = false,defaultValue = "-1") Integer school) {
		return Result.success(collegeService.ListCollegeWithSchool(count, page, school, false));
	}

	@ApiOperation("用户搜索院系列表")
	@RequestMapping(value = "user/colleges/{page}/{count}", method = RequestMethod.GET)
	public Result UserListCollege(@PathVariable("page") Integer page, @PathVariable("count") Integer count,
			@RequestParam(value = "school",required = false,defaultValue = "-1") Integer school) {
		return Result.success(collegeService.ListCollegeWithSchool(count, page, school, true));
	}
}
